var express = require('express');
var router = express.Router();
var article = require('../service/articleService');
var category = require('../service/categoryService');
var siteCfg = require('../config/base');
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', {
      title: siteCfg.siteTitle,
      discribe: siteCfg.describe
  });
});

router.get('/article/:articleId',article.getArticleByIndex, function(req, res, next) {
    //取单页面数据
    console.log('通过文章id取单页面数据');
    res.render('article', { title: req.articleId });
});

router.get('/category/:categoryId',category.getCategoryByIndex, function(req, res, next) {
    //取文章列表页面数据
    console.log('通过栏目id取文章列表数据');
    res.render('category', { title: req.categoryId });
});

router.param('articleId', function(req, res, next, id){
    if (id) {
        req.articleId = id;
        next();
    } else {
        next(new Error('找不到文章'));
    }
});

router.param('categoryId', function(req, res, next, id){
    if (id) {
        console.log(id);
        req.categoryId = id;
        next();
    } else {
        next(new Error('找不到栏目'));
    }
});

module.exports = router;
